package Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Test14 {
    //买股票的最佳时机
    public int maxProfit(int[] prices) {
        int ret = 0;
        int n = prices.length;
        for(int i = 0;i<n-1;i++){
            if(prices[i] < prices[i+1]) ret += (prices[i+1]-prices[i]);
        }
        return ret;
    }
    //贪心(跳跃游戏)
    public boolean canJump(int[] nums) {
        int n = nums.length;
        //定义可以到达的最大位置
        int max = 0;
        for(int i = 0;i<n;i++){
            //i是自己本来的位置,小于最大位置的时候,才能移动
            if(i <= max){
                max = Math.max(max,i+nums[i]);
                if(max >= n - 1) return true;
            }
        }
        return false;

    }
    //下降路径的最小和
    public int maxValue(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m+1][n+1];
        for(int i = 1;i<=m;i++){
            for(int j = 1;j<=n;j++){
                dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+grid[i-1][j-1];
            }
        }
        return dp[m][n];
    }
    static boolean[] sta;
    static int ret;
    static ArrayList<Integer> list = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
//        sta = new boolean[n];
//        count = n;
        int[] arr = new int[n];
        for(int i = 0;i<n;i++){
            arr[i] = sc.nextInt();
            list.add(arr[i]);
        }

        func2();

        System.out.println(ret);
    }
    public static void main1(String[] args) {
        int[] arr = new int[]{1,3,5,2,4,6};
        bubbSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    static void bubbSort(int[] arr){
        int n = arr.length;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n-i-1;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    static int func(int n){
        if(n <= 2) return 1;
        return func(n-1) + func(n-2);
    }
    static void func2(){
        int k = 1;
        int i = 0;
        int m = 0;
//        for(;m<list.size();m++){
//            if(list.get(m) == 1){
//                list.remove(m);
//                break;
//            }
//        }
//        i = m-1;
        int count = 0;
        while(list.size() > 1){
//            k++;
            i%=list.size();
//            if(count == 0){
//
//            }
            if(list.get(i) == k){
                list.remove(i);
//                i++;
                k++;
                k%=4;
            } else {
                i++;
            }

        }
        ret = list.get(0);
    }
}
